package com.finalproject.entity;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class VendingMachineDatabase {
	
	/////NOTE: remove by id?
	Gson gson=new Gson();

	
	private List<VendingMachine> vendingMachines;
	
	
	private static VendingMachineDatabase INSTANCE;
	
	public static VendingMachineDatabase getInstance() {
		if(INSTANCE == null) {
			INSTANCE = new VendingMachineDatabase();
		}
		return INSTANCE;
	}
	
	private VendingMachineDatabase() {
		this.vendingMachines = new ArrayList<VendingMachine>();
//		initFromMemory();
		try {
			initFromFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public List<VendingMachine> getVendingMachines() {
		return vendingMachines;
	}
	
	public VendingMachine getIndex(int index) {
		return vendingMachines.get(index);
	}
	
	private void initFromFile() throws IOException{
		String str=readFile("vending_database.txt");
		Type myType=new TypeToken<List<VendingMachine>>() {}.getType();
		vendingMachines=gson.fromJson(str, myType);
	
	}
	private String readFile(String pathname) throws IOException {

	    File file = new File(pathname);
	    StringBuilder fileContents = new StringBuilder((int)file.length());
	    Scanner scanner = new Scanner(file);
	    String lineSeparator = System.getProperty("line.separator");

	    try {
	        while(scanner.hasNextLine()) {        
	            fileContents.append(scanner.nextLine() + lineSeparator);
	        }
	        return fileContents.toString();
	    } finally {
	        scanner.close();
	    }
	}
	
	private void initFromMemory() {
		
		VendingMachine v1 = new VendingMachine(101,new int[] {95,125});
		v1.add(new SnackItem("KitKat", 1.00, "KitKatis a chocolate-coated wafer confection ", 218, 11.0,5.0, 27.0, 23.0,3.0),10);
		v1.add(new SnackItem("Snickers", 1.00, "Handle your Hunger", 271, 13.6,7.0, 34.5,140.0,3.0),10);
		v1.add(new SnackItem("Nature Valley", 1.00, "A Healter Bar", 180, 6.0,0.0, 29.0,160.0,4.0),10);
		
		v1.add(new SnackItem("Planters Peanut Dry Roasted", 1.00, "Delecious snackbar", 160, 5.0, 0.0, 27.0, 190.0,7.0),10);
		v1.add(new SnackItem("Sunchips Original", 1.00, "A Healthy Chips", 137, 6.0,0.0, 19.0, 92.0,2.0),10);
		v1.add(new SnackItem("M&M Candy", 1.00, "Delecious snackbar", 138, 6.0, 20.0, 5.0,17.0,1.0),10);
		
		v1.add(new SnackItem("OREOS", 1.20, "Chocolate Cookies", 260, 13.0, 31.0,5.0, 31.0,5.0),10);
		v1.add(new SnackItem("Lays Chips", 1.00, "Original Lay Chips", 150, 10.0, 3.0, 15.0, 180.0,2.0),10);
		v1.add(new SnackItem("Ritz", 1.00, "Ritz Crackers Original", 80, 5.0, 10.0, 4.0,105.0,1.0),10);
//		
//		
		vendingMachines.add(v1);
		VendingMachine v2 = new VendingMachine(102,new int[] {20,300});
		
		v2.add(new SnackItem("OREOS", 1.20, "Chocolate Cookies", 260, 13.0, 31.0,0.0, 31.0,5.0),10);
		v2.add(new SnackItem("Lays Chips", 1.00, "Original Lay Chips", 150, 10.0, 15.0,0.0, 180.0,2.0),10);
		v2.add(new SnackItem("Ritz", 1.00, "Ritz Crackers Original", 80, 5.0, 10.0, 3.0,105.0,1.0),10);
//		
		v2.add(new SnackItem("Nabisco Chip Ahoy", 1.00, " Nabisco Chips Ahoy ~ Thin Crisps", 100, 3.0, 0.0, 18.0, 140.0,1.0),10);
		v2.add(new SnackItem("DASANI Spring Winter ", 1.00, "Water, bottled, non-carbonated, DASANI", 0, 0.0, 0.0,0.0, 0.0,0.0),10);
		v2.add(new SnackItem("Diet Code ", 1.00, "Diet Code", 0, 0.0, 0.0,0.0, 30.0,0.0),10);
	
		vendingMachines.add(v2);
		
		
		
		VendingMachine v3 = new VendingMachine(103, new int[] {250,250});
		v3.add(new SnackItem("KitKat", 1.00, "KitKatis a chocolate-coated wafer confection ", 218, 11.0,5.0, 27.0, 23.0,3.0),10);
		v3.add(new SnackItem("Snickers", 1.00, "Handle your Hunger", 271, 13.6,7.0, 34.5,140.0,3.0),10);
		v3.add(new SnackItem("Nature Valley", 1.00, "A Healter Bar", 180, 6.0,0.0, 29.0,160.0,4.0),10);
		
		v3.add(new SnackItem("Planters Peanut Dry Roasted", 1.00, "Delecious snackbar", 160, 5.0, 0.0, 27.0, 190.0,7.0),10);
		v3.add(new SnackItem("Sunchips Original", 1.00, "A Healthy Chips", 137, 6.0,0.0, 19.0, 92.0,2.0),10);
		v3.add(new SnackItem("M&M Candy", 1.00, "Delecious snackbar", 138, 6.0, 20.0, 5.0,17.0,1.0),10);
		
		v3.add(new SnackItem("OREOS", 1.20, "Chocolate Cookies", 260, 13.0, 31.0,5.0, 31.0,5.0),10);
		v3.add(new SnackItem("Lays Chips", 1.00, "Original Lay Chips", 150, 10.0, 3.0, 15.0, 180.0,2.0),10);
		v3.add(new SnackItem("Ritz", 1.00, "Ritz Crackers Original", 80, 5.0, 10.0, 4.0,105.0,1.0),10);
//		
//		
		vendingMachines.add(v3);
		VendingMachine v4 = new VendingMachine(104, new int[] {300,400});
		
		v4.add(new SnackItem("OREOS", 1.20, "Chocolate Cookies", 260, 13.0, 31.0,0.0, 31.0,5.0),10);
		v4.add(new SnackItem("Lays Chips", 1.00, "Original Lay Chips", 150, 10.0, 15.0,0.0, 180.0,2.0),10);
		v4.add(new SnackItem("Ritz", 1.00, "Ritz Crackers Original", 80, 5.0, 10.0, 3.0,105.0,1.0),10);
//		
		v4.add(new SnackItem("Nabisco Chip Ahoy", 1.00, " Nabisco Chips Ahoy ~ Thin Crisps", 100, 3.0, 0.0, 18.0, 140.0,1.0),10);
		v4.add(new SnackItem("DASANI Spring Winter ", 1.00, "Water, bottled, non-carbonated, DASANI", 0, 0.0, 0.0,0.0, 0.0,0.0),10);
		v4.add(new SnackItem("Diet Code ", 1.00, "Diet Code", 0, 0.0, 0.0,0.0, 30.0,0.0),10);
	
		vendingMachines.add(v4);
		
		
		
	}
	
	
	public void add(VendingMachine vendingMachine) {
		vendingMachines.add(vendingMachine);
	}
	
	
	public void remove(VendingMachine vendingMachine) {
		vendingMachines.remove(vendingMachine);
	}
	
}
